package org.apache.oozie.tools.workflowgenerator.client.property.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.oozie.tools.workflowgenerator.client.property.PropertyTable;
import org.apache.oozie.tools.workflowgenerator.client.widget.NodeWidget;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.SelectionCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.cell.client.TextInputCell;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;

public class FSPropertyTable extends PropertyTable {

    protected Grid grid;
    protected List<FSActionData> fsdata;

    public FSPropertyTable(NodeWidget w) {
        super(w);
        initConf();
        initWidget();
    }

    /**
     * Initialize configuration
     */
    private void initConf() {
        fsdata = new ArrayList<FSActionData>();
        fsdata.add(new FSActionData("", "", null, null));

        widgetList = new ArrayList<NodeWidget>();
        updateWidgetList();
        widgetDropDown = new ArrayList<NodeWidget>();
        updateWidgetDropDown();
    }

    /**
     * Initialize widgets shown in property table
     */
    protected void initWidget() {

        VerticalPanel vertical = new VerticalPanel();
        this.add(vertical);
        grid = new Grid(4, 2);
        vertical.add(grid);

        this.setAlwaysShowScrollBars(true);
        this.setSize("100%", "80%");

        // insert row for Name
        grid.setWidget(0, 0, createLabel("Name"));
        name = new TextBox();
        name.setText("FS"); // default value
        grid.setWidget(0, 1, formatCell(name));

        // insert row for OK
        grid.setWidget(1, 0, createLabel("OK"));
        okVal = new ListBox();
        createWidgetDropDownList(okVal);
        grid.setWidget(1, 1, formatCell(okVal));

        // insert row for ERROR
        grid.setWidget(2, 0, createLabel("Error"));
        errorVal = new ListBox();
        updateErrorDropDownList();
        grid.setWidget(2, 1, formatCell(errorVal));

        // create cell table for FS operation
        vertical.add(createFSActionTable(fsdata));
    }

    /**
     * Create a table showing fs operations
     *
     * @param data
     * @return
     */
    protected CellTable<FSActionData> createFSActionTable(List<FSActionData> data) {

        final CellTable<FSActionData> table = new CellTable<FSActionData>();
        final ListDataProvider<FSActionData> dataProvider = new ListDataProvider<FSActionData>();
        dataProvider.setList(data);
        dataProvider.addDataDisplay(table);

        // Add Name column
        Column<FSActionData, String> nameCol = null;

        nameCol = new Column<FSActionData, String>(new SelectionCell(Arrays.asList("", "delete", "mkdir", "move",
                "chmod"))) {
            @Override
            public String getValue(FSActionData object) {
                return object.getOp();
            }
        };

        // set event for updating value
        nameCol.setFieldUpdater(new FieldUpdater<FSActionData, String>() {
            @Override
            public void update(int index, FSActionData object, String value) {
                FSActionData d = dataProvider.getList().get(index);
                d.setOp(value);
                table.redraw();
            }
        });
        table.addColumn(nameCol, "operation");

        Column<FSActionData, String> label1Col = new Column<FSActionData, String>(new TextCell()) {
            @Override
            public String getValue(FSActionData object) {
                String rel = "Path";
                String op = object.getOp();
                if (op.equals("move")) {
                    rel = "Source Path";
                }
                return rel;
            }
        };

        table.addColumn(label1Col);

        // Add Column for 1st parameter of delete/mkdir/chmod/move
        Column<FSActionData, String> param1Col = new Column<FSActionData, String>(new TextInputCell()) {
            @Override
            public String getValue(FSActionData object) {
                String op = object.getOp();
                if (op.equals("delete") || op.equals("mkdir") || op.equals("chmod")) {
                    if (object.getParams().containsKey("path") && object.getParams().get("path") != null)
                        return object.getParams().get("path");
                }
                else if (op.equals("move")) {
                    if (object.getParams().containsKey("source") && object.getParams().get("source") != null)
                        return object.getParams().get("source");
                }
                return "";
            }
        };

        param1Col.setFieldUpdater(new FieldUpdater<FSActionData, String>() {
            @Override
            public void update(int index, FSActionData object, String value) {
                FSActionData d = dataProvider.getList().get(index);
                String op = d.getOp();
                if (op.equals("delete") || op.equals("mkdir") || op.equals("chmod")) {
                    d.getParams().put("path", value);
                }
                else if (op.equals("move"))
                    d.getParams().put("source", value);
            }
        });
        table.addColumn(param1Col, "");

        Column<FSActionData, String> label2Col = new Column<FSActionData, String>(new TextCell()) {

            public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
                if (value != null) {
                    FSActionData data = (FSActionData) context.getKey();
                    if (data.getOp().equals("move") || data.getOp().equals("chmod"))
                        sb.append(value);
                }
            }

            @Override
            public String getValue(FSActionData object) {
                String rel = null;
                String op = object.getOp();
                if (op.equals("move")) {
                    rel = "Target Path";
                }
                else if (op.equals("chmod")) {
                    rel = "Permissions";
                }
                return rel;
            }
        };

        table.addColumn(label2Col);

        // Add Column for 2nd parameter of move and chmod
        Column<FSActionData, String> param2Col = new Column<FSActionData, String>(new CustomEditTextCell()) {
            @Override
            public String getValue(FSActionData object) {
                String op = object.getOp();
                if (op.equals("move")) {
                    if (object.getParams().containsKey("target") && object.getParams().get("target") != null)
                        return object.getParams().get("target");
                }
                else if (op.equals("chmod")) {
                    if (object.getParams().containsKey("permissions") && object.getParams().get("permissions") != null)
                        return object.getParams().get("permissions");
                }
                return "";
            }
        };

        param2Col.setFieldUpdater(new FieldUpdater<FSActionData, String>() {
            @Override
            public void update(int index, FSActionData object, String value) {
                FSActionData d = dataProvider.getList().get(index);
                String op = d.getOp();
                if (op.equals("move")) {
                    d.getParams().put("target", value);
                }
                else if (op.equals("chmod"))
                    d.getParams().put("permissions", value);
            }
        });
        table.addColumn(param2Col, "");

        Column<FSActionData, String> label3Col = new Column<FSActionData, String>(new TextCell()) {

            public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
                if (value != null) {
                    FSActionData data = (FSActionData) context.getKey();
                    if (data.getOp().equals("chmod"))
                        sb.append(value);
                }
            }

            @Override
            public String getValue(FSActionData object) {
                String rel = null;
                String op = object.getOp();
                if (op.equals("chmod"))
                    rel = "Chmod files within directory?(dir-files)";
                return rel;
            }
        };

        table.addColumn(label3Col);

        // Add Column for 3rd parameter of chmod
        Column<FSActionData, String> param3Col = new Column<FSActionData, String>(new CustomSelectionCell(
                Arrays.asList("true", "false"))) {
            @Override
            public String getValue(FSActionData object) {
                String rel = null;
                String op = object.getOp();
                if (op.equals("chmod"))
                    rel = object.getParams().get("dir-files");
                return rel;
            }
        };

        param3Col.setFieldUpdater(new FieldUpdater<FSActionData, String>() {
            @Override
            public void update(int index, FSActionData object, String value) {
                FSActionData d = dataProvider.getList().get(index);
                String op = d.getOp();
                if (op.equals("chmod"))
                    d.getParams().put("dir-files", value);
            }
        });
        table.addColumn(param3Col, "");

        // Button to add row
        Column<FSActionData, String> addCol = new Column<FSActionData, String>(new ButtonCell()) {
            @Override
            public String getValue(FSActionData object) {
                return " + ";
            }
        };
        addCol.setFieldUpdater(new FieldUpdater<FSActionData, String>() {
            @Override
            public void update(int index, FSActionData object, String value) {
                dataProvider.getList().add(index + 1, new FSActionData("", "", "", ""));
            }
        });

        table.addColumn(addCol, "");

        // Button to delete row
        Column<FSActionData, String> delCol = new Column<FSActionData, String>(new ButtonCell()) {
            @Override
            public String getValue(FSActionData object) {
                return " - ";
            }
        };

        delCol.setFieldUpdater(new FieldUpdater<FSActionData, String>() {

            @Override
            public void update(int index, FSActionData object, String value) {
                List<FSActionData> li = dataProvider.getList();
                if (li.size() == 1) {
                    FSActionData p = li.get(0);
                    p.clear();
                    table.redraw();
                }
                else
                    dataProvider.getList().remove(index);
            }
        });

        table.addColumn(delCol, "");

        return table;
    }

    class CustomEditTextCell extends TextInputCell {

        @Override
        public void render(Context context, String value, SafeHtmlBuilder sb) {
            FSActionData fsdata = (FSActionData) context.getKey();
            String op = fsdata.getOp();
            if (op.equals("move") || op.equals("chmod"))
                super.render(context, value, sb);

        }
    }

    class CustomSelectionCell extends SelectionCell {

        public CustomSelectionCell(List<String> options) {
            super(options);
        }

        @Override
        public void render(Context context, String value, SafeHtmlBuilder sb) {
            FSActionData fsdata = (FSActionData) context.getKey();
            String op = fsdata.getOp();
            if (op.equals("chmod"))
                super.render(context, value, sb);
        }
    }

    /**
     * Generate xml elements of fs action and attach them to xml doc
     */
    public void generateXML(Document doc, Element root, NodeWidget next) {

        Element action = doc.createElement("action");
        action.setAttribute("name", current.getName());

        // create <delete>
        fsDataListToXML(fsdata, action, doc, "delete");

        // create <mkdir>
        fsDataListToXML(fsdata, action, doc, "mkdir");

        // create <move>
        fsDataListToXML(fsdata, action, doc, "move");

        // create <chmod>
        fsDataListToXML(fsdata, action, doc, "chmod");

        Element okele = doc.createElement("ok");
        okele.setAttribute("to", next.getName());
        action.appendChild(okele);

        Element errele = doc.createElement("error");
        NodeWidget kill = getKillNode();
        errele.setAttribute("to", kill == null ? "" : kill.getName());
        action.appendChild(errele);

        root.appendChild(action);

    }

    /**
     * Generate xml elements of fs operations
     *
     * @param list list of FS operations
     * @param root xml element under which fs action is added
     * @param doc xml document
     * @param key name of fs operation
     */
    protected void fsDataListToXML(List<FSActionData> list, Element root, Document doc, String key) {

        for (FSActionData fsdata : list) {
            String op = fsdata.getOp();

            if (op != null && !op.matches("\\s*") && op.equals(key)) {

                Map<String, String> params = fsdata.getParams();

                boolean flag = true;
                if (op.equals("delete") && !params.containsKey("path")) {
                    flag = false;
                }
                else if (op.equals("mkdir") && !params.containsKey("path")) {
                    flag = false;
                }
                else if (op.equals("move") && (!params.containsKey("source") || !params.containsKey("target"))) {
                    flag = false;
                }
                else if (op.equals("chmod") && (!params.containsKey("path") || !params.containsKey("permissions"))) {
                    flag = false;
                }

                if (flag) {
                    // create key element
                    Element nameele = doc.createElement(key);
                    root.appendChild(nameele);

                    // set attribute for parameter(s)
                    for (Map.Entry<String, String> e : params.entrySet()) {
                        nameele.setAttribute(e.getKey(), e.getValue());
                    }
                }
            }
        }

    }

    /**
     * class to store FS operation and relevant parameters
     *
     * @author egashira
     */
    public class FSActionData {

        protected String op;
        protected Map<String, String> params;

        public FSActionData(String op, String param0, String param1, String param2) {
            params = new HashMap<String, String>();
            this.op = op;
            if (op.equals("delete")) {
                params.put("path", param0);
            }
            else if (op.equals("mkdir")) {
                params.put("path", param0);
            }
            else if (op.equals("move")) {
                params.put("source", param0);
                params.put("target", param1);
            }
            else if (op.equals("chmod")) {
                params.put("path", param0);
                params.put("permissions", param1);
                params.put("dir-files", param2);
            }
        }

        public String getOp() {
            return op;
        }

        public void setOp(String s) {
            this.op = s;
        }

        public Map<String, String> getParams() {
            return params;
        }

        public void clear() {
            op = "";
            params.clear();
        }
    }

}
